<template>
    <div class="box">
        <div class="title">权限范围</div>
        <nav>
            <van-checkbox-group v-model="result">
                <ul>
                    <li v-for="(data,index) in list" :key="index">
                        <van-checkbox @click="handleClick(data.id)" :name="data.id">
                            {{data.businessName}}
                            <template #icon="props">
                                <img class="img-icon" :src="props.checked ? 'img/xuankuanga@2x.png' : 'img/weikuangkuang@2x.png'" />
                            </template>
                            </van-checkbox>
                    </li>
                </ul> 
            </van-checkbox-group>
        </nav>
         <!-- 底部按钮 -->
        <footer>
            <!-- <button>确定</button> -->
        </footer>
    </div>
</template>
<script>
export default {
    data() {
        return {
            result:[],
            list:[],
            userId:'',
            userName:'',
            courseId:'',
            courseName:'',
            adminName:''
        }
    },
    methods: {
        handleClick(id){
            (async ()=>{
                 let values = await this.get('/user/userInfoShow',{userId:localStorage.getItem('id')});
                 this.adminName = values.data.userName;
            })()
            let classPartName = this.list.filter(item=>item.id == id)[0].businessName
            let obj = {
                    classPartId:id,
                    userId:this.userId,
                    adminId:localStorage.getItem('id'),
                    adminName:this.adminName,
                    userName:this.userName,
                    classPartName
                }
            if(this.result.some(item=>item == id)){
                (async ()=>{
                    let data = await this.get('/course/insertCourseAccessUserAndClassPrat',obj)
                    console.log(data);
                })()
            }else {
                (async ()=>{
                    let data = await this.get('/course/deleteCourseAccessUserAndClassPrat',obj)
                    console.log(data);
                })()
            }
        }
    },
    mounted() {
        this.userId = this.$route.query.userId;
        this.userName = this.$route.query.userName;
        this.courseId = this.$route.query.courseId;
        this.courseName = this.$route.query.courseName;
        (async ()=>{
            let { data } = await this.get('/course/queryCourseAccess',{userId:this.userId,courseId:this.courseId})
            this.list = data.all;
            this.result = data.other.map(item=>item.id);
        })()
    },
}
</script>
<style lang="scss" scoped>
 .img-icon{
        width: 0.24rem;
        height: 0.24rem;
    }
    ul{
        padding: 0 0.2rem;
        box-sizing: border-box;
       font-size: 0.28rem;
       li{
           padding: 0.2rem 0;
           box-sizing: border-box;
           border-bottom: 1px solid #eee;
       }
    }
.box{
    display: flex;
    flex-direction: column;
    height: 100%;
}
    .title{
        padding: 0.24rem;
        box-sizing: border-box;
        font-size: 0.3rem;
        font-family: PingFang SC;
        font-weight: bold;
        color: #343434;
        background:#F7F9FA;
        // line-height: 34px;
    }
    footer{
        padding: 0 0.24rem 0.24rem;
        box-sizing: border-box;
        background: white;
        button{
            width: 100%;
            height: 0.84rem;
            background: #2E9EFF;
            font-size: 0.33rem;
            font-family: PingFang SC;
            font-weight: bold;
            color: #FFFFFF;
            border: none;
            box-shadow: 1px 0.12rem 0.12rem 0.02rem rgba(4, 111, 204, 0.07);
            border-radius: 0.08rem;
        }
    }
    nav{
        flex: 1;
        overflow: scroll;
        background: white;
    }
</style>